//
//  MBOrderGoodsTableViewCell.m
//  MemberBusiness-ios
//
//  Created by cherry pu on 2018/4/24.
//  Copyright © 2018年 zhangXin. All rights reserved.
//

#import "MBOrderGoodsTableViewCell.h"

@interface MBOrderGoodsTableViewCell()
@property (nonatomic,weak)UIImageView* goodsImageView;
@property (nonatomic,weak)UILabel* nameLabel;
@property (nonatomic,weak)UILabel* moneyLabel;
@property (nonatomic,weak)UILabel* countLabel;

@end

@implementation MBOrderGoodsTableViewCell
@dynamic viewModel;

-(void)setupLayout
{
    [super setupLayout];
    
    self.backgroundColor = [UIColor colorWithR:250 G:250 B:250];
    
    UIImageView* goodsImageView = [[UIImageView alloc]init];
    self.goodsImageView = goodsImageView;
    goodsImageView.image = [UIImage imageFromColor:COLOR_MAIN forSize:CGSizeMake([UIView lf_sizeFromIphone6:80], [UIView lf_sizeFromIphone6:80]) withCornerRadius:[UIView lf_sizeFromIphone6:6]];
    [self.contentView addSubview:goodsImageView];
    
    [goodsImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.contentView);
        make.left.equalTo(self.contentView).offset([UIView lf_sizeFromIphone6:12]);
        make.size.mas_equalTo(CGSizeMake([UIView lf_sizeFromIphone6:80], [UIView lf_sizeFromIphone6:80]));
    }];
    
    UILabel* nameLabel = [[UILabel alloc]init];
    self.nameLabel = nameLabel;
    nameLabel.text = @"华为荣耀双卡双待双通 移动联通电信4G豪华套装1";
    nameLabel.font = [UIFont lf_systemFontOfSize:14];
    nameLabel.textColor = [UIColor colorWithR:24 G:24 B:24];
    nameLabel.numberOfLines = 2;
    [self.contentView addSubview:nameLabel];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(goodsImageView).offset([UIView lf_sizeFromIphone6:5]);
        make.left.equalTo(goodsImageView.mas_right).offset([UIView lf_sizeFromIphone6:10]);
        make.right.equalTo(self.contentView).offset([UIView lf_sizeFromIphone6:-25]);
    }];

    UILabel* moneyLabel = [[UILabel alloc]init];
    self.moneyLabel = moneyLabel;
    moneyLabel.text = @"¥511.00";
    moneyLabel.font = [UIFont lf_systemFontOfSize:14];
    moneyLabel.textColor = [UIColor colorWithR:255 G:0 B:0];
    [self.contentView addSubview:moneyLabel];
    
    [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.bottom.equalTo(goodsImageView).offset([UIView lf_sizeFromIphone6:-5]);
    }];

    UILabel* countLabel = [[UILabel alloc]init];
    self.countLabel = countLabel;
    countLabel.text = @"×1";
    countLabel.font = [UIFont lf_systemFontOfSize:14];
    countLabel.textColor = [UIColor colorWithR:153 G:153 B:153];
    [self.contentView addSubview:countLabel];
    
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(moneyLabel);
        make.right.equalTo(self.contentView).offset([UIView lf_sizeFromIphone6:-12]);
    }];

}

// 用来计算高度
+(NSNumber*)calculateRowHeightWithViewModel:(MBOrderTableCellViewModel *)viewModel
{
    return [viewModel cacheCellHeightWithCalculateBlock:^NSNumber * _Nonnull{
        return viewModel.totalHeight;
    }];
}

-(void)setGoodsModel:(MBOrderGoodsModel *)goodsModel
{
    _goodsModel = goodsModel;
    
    NSString* cacheUrl = [NSString stringWithFormat:@"%ld",(long)goodsModel.goods_id];
    UIImage* image = [[MBCacheService sharedInstance]fetchHeaderWithHeaderName:cacheUrl];
    
    if (image) {
        self.goodsImageView.image = image;
    }else {
        [self.goodsImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/goods/goodsThumImages/goods_id/%ld/width/200/height/200",BASE_URL,(long)goodsModel.goods_id]] placeholderImage:[UIImage imageFromColor:COLOR_BG forSize:CGSizeMake([UIView lf_sizeFromIphone6:90], [UIView lf_sizeFromIphone6:90]) withCornerRadius:[UIView lf_sizeFromIphone6:6]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            if (image) {
                image = [image imageWithCornerRadius:[UIView lf_sizeFromIphone6:6]];
                self.goodsImageView.image = image;
                [[MBCacheService sharedInstance]cacheHeaderWithHeaderName:cacheUrl withHeader:image];
            }
        }];
    }
    
    self.nameLabel.text = goodsModel.goods_name;
    self.moneyLabel.text = [NSString stringWithFormat:@"￥%@",goodsModel.goods_price];
    self.countLabel.text = [NSString stringWithFormat:@"×%d",goodsModel.goods_num];

}

@end
